package com.rightsidetech.http.wechatutil;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.utils.HttpClient;
import com.rightsidetech.http.config.WechatConfig;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class WeChatRefundUtils {

    /**
     * 微信原路退回
     * @param orderNo 订单号
     * @param amount 金额
     * @param transactionId 第三方订单号
     * @param totalFee 总订单金额
     * @return
     * @throws Exception
     */
    public static ResponseData weichatOriginalRefund(WechatConfig wechatConfig, String orderNo, BigDecimal amount, String transactionId,String totalFee) throws Exception{
        try {
            HttpClient httpClient = new HttpClient(WechatConfig.REFUND_URL, 3000, 3000);
            int refundFee = amount.intValue();
            String xml = WechatUtil.genRefundReq(wechatConfig.getAppId(), wechatConfig.getApiKey(), wechatConfig.getMchId(), transactionId, orderNo, totalFee, Integer.toString(refundFee), "CYN");
            log.info("微信支付:商户订单号" + orderNo + ",请求参数:" + xml);
            xml = httpClient.sendXmlCert(xml, "UTF-8", wechatConfig.getRefundCertPath(), wechatConfig.getMchId());
            if (!StringUtils.isEmpty(xml)) {
                Map<String, Object> map = WechatUtil.getMapFromXML(xml);
                if (map.get("return_code").equals("SUCCESS")) {
                    if (map.get("result_code").equals("SUCCESS")) {
                        log.info("微信支付:商户订单号" + orderNo + "退款成功,map={}", JSONObject.toJSONString(map));
                        Map map1 = new HashMap();
                        map1.put("orderNo", orderNo);
                        map1.put("thrid_orderNo", map.get("refund_id"));
                        return ResponseData.success(map1);
                    } else{
                        if (map.get("err_code").equals("TRADE_OVERDUE")) {
                            log.info("微信支付:商户订单号"+ orderNo +"已经超过退款期限");
                            return ResponseData.fail("微信支付:商户订单号" + orderNo + "已经超过退款期限");
                        }
                        String errCodeDes = String.valueOf(map.get("err_code_des"));
                        String errCode = String.valueOf(map.get("err_code"));
                        log.info("微信支付:商户订单号" + orderNo +"退款失败,原因:" + errCodeDes);
                        return ResponseData.fail("微信支付:商户订单号" + orderNo +"退款失败,原因:" + errCodeDes);
                    }
                } else {
                    String returnMsg = String.valueOf(map.get("return_msg"));
                    String returnCode = String.valueOf(map.get("return_code"));
                    log.info("微信支付:商户订单号" + orderNo +"退款失败,原因:" + returnMsg);
                    return ResponseData.fail("微信支付:商户订单号" + orderNo +"退款失败,原因:" + returnMsg);
                }
            }
            log.info("微信支付:商户订单号" + orderNo +"退款失败,微信返回信息为空");
        } catch (Exception e) {
            log.error("微信支付:微信退款接口调用错误", e);
        }
        return ResponseData.fail("异常报错");
    }

}
